package com.zqf.testjzvideoplayer.jzvideo;

import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zqf.testjzvideoplayer.MainActivity;
import com.zqf.testjzvideoplayer.R;

import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;

public class DetailVideoActivityJava extends AppCompatActivity {
    public final long mDURATION = 250;
    private FrameLayout container;
    private ViewAttr attr;
    private ViewAttr currentAttr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JZUtils.hideSystemUI(this);
        JZUtils.hideStatusBar(this);
        setContentView(R.layout.activity_detail);
        attr = getIntent().getParcelableExtra("attr");
        container = findViewById(R.id.surface_container);
        container.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                container.getViewTreeObserver().removeOnPreDrawListener(this);
                ViewParent parent = JzVdStdRv.CURRENT_JZVD.getParent();
                if (parent != null) {
                    ((ViewGroup) parent).removeView(JzVdStdRv.CURRENT_JZVD);
                }
                container.addView(JzVdStdRv.CURRENT_JZVD, new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                currentAttr = new ViewAttr();
                int[] location = new int[2];
                container.getLocationInWindow(location);
                currentAttr.setX(location[0]);
                currentAttr.setY(location[1]);
                currentAttr.setWidth(container.getMeasuredWidth());
                currentAttr.setHeight(container.getMeasuredHeight());
                new ViewMoveHelper(container, attr, currentAttr, mDURATION).startAnim();
                AlphaAnimation animation = new AlphaAnimation(0, 1);
                animation.setDuration(mDURATION);
                animation.start();
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) return;
        backAnimation();
    }

    @Override
    protected void onResume() {
        Jzvd.goOnPlayOnResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
    }

    private void backAnimation() {
        new ViewMoveHelper(container, currentAttr, attr, mDURATION).startAnim();
        container.postDelayed(() -> {
            if (MainActivity.mainActivity != null) {
                MainActivity.mainActivity.animateFinish();
            }
            finish();
            overridePendingTransition(0, 0);
        }, mDURATION);

    }


}
